package com.ffcs.cn.action.base.checkbox;

import com.supermap.desktop.controls.ui.controls.SmDialog;
import com.supermap.desktop.controls.ui.controls.comboBox.SmComboBoxMulitSelect;
import com.supermap.desktop.core.ui.controls.GridBagConstraintsHelper;

import javax.swing.*;
import java.awt.*;
import java.util.List;

public class DialogJComboBoxMultiSelect extends SmDialog {
	private JLabel label;
	private SmComboBoxMulitSelect<ComboBoxDataItem> comboBoxMulitSelect;

	public DialogJComboBoxMultiSelect() {
		//设置标题
		setTitle("JComboBox 多选");
		//设置初始化窗口大小
		Dimension dimension = new Dimension(300, 400);
		setSize(dimension);
		//设置最小窗口
		setMinimumSize(dimension);
		//初始化使用控件
		initComponents();
		//初始化布局，并添加控件
		initLayout();
		// 注册事件
		registerEvents();
	}

	private void initComponents() {
		this.label = new JLabel("多选:");
		// 创建多选ComboBox，并添加数据，注意添加数据需要通过addDefaultItem方法，同时需要将数据对应的渲染的文本作为方法第二个参数传入
		this.comboBoxMulitSelect = new SmComboBoxMulitSelect<>();
		for (int i = 0; i < 10; i++) {
			ComboBoxDataItem comboBoxDataItem = new ComboBoxDataItem(String.valueOf(10000 + i));
			this.comboBoxMulitSelect.addDefaultItem(comboBoxDataItem,comboBoxDataItem.getValue());
		}
	}

	private void initLayout(){
		//设置布局为 GridBagLayout 布局
		this.setLayout(new GridBagLayout());

		this.add(this.label, new GridBagConstraintsHelper(0, 0, 1, 1).setAnchor(GridBagConstraints.WEST).setInsets(GridBagConstraintsHelper.FRAME_CONTROL_GAP, GridBagConstraintsHelper.FRAME_CONTROL_GAP, GridBagConstraintsHelper.CONTROLS_GAP, 0));
		this.add(this.comboBoxMulitSelect, new GridBagConstraintsHelper(1, 0, 1, 1).setAnchor(GridBagConstraints.EAST).setInsets(GridBagConstraintsHelper.FRAME_CONTROL_GAP, GridBagConstraintsHelper.CONTROLS_GAP, GridBagConstraintsHelper.CONTROLS_GAP, GridBagConstraintsHelper.FRAME_CONTROL_GAP).setWeight(1, 0).setFill(GridBagConstraints.HORIZONTAL));

		this.add(new JPanel(), new GridBagConstraintsHelper(0, 1, 2, 1).setInsets(0, GridBagConstraintsHelper.CONTROLS_GAP, 0, GridBagConstraintsHelper.FRAME_CONTROL_GAP).setFill(GridBagConstraints.BOTH).setWeight(1, 1));
	}

	private void registerEvents(){
		this.comboBoxMulitSelect.addSelectedChangedListener(this.selectedChangedListener);
	}

	// 添加选择项实时改变事件，可以从选择项时时获取到数据
	private SmComboBoxMulitSelect.SelectedChangedListener selectedChangedListener = new SmComboBoxMulitSelect.SelectedChangedListener() {
		@Override
		public void changed() {
			List<ComboBoxDataItem> selectedItems = comboBoxMulitSelect.getSelectedItems();
		}
	};
}
